package com.infosys.mfg;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class EmployeeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String CONTENT_TYPE = "text/html; charset=windows-1252";

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();
		out.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		
		Enumeration paramNames = request.getParameterNames();
		while(paramNames.hasMoreElements()){
			System.out.println("param: "+paramNames.nextElement());
		}
		
		String name = request.getParameter("name");
		if(name != null && name.trim().length() > 0 ){
			System.out.println("reached post!" + request.getParameter("name"));
			out.println("reached post!\n");
			out.println("Your posted content in upper case: "+name.toUpperCase());
		}else{
			// flag error!
			response.setHeader("errorMsg", "Did you forget to input name?");
			response.sendError(500);
		}
		out.flush();
		out.close();
	}
}
